//
//  MyInformationViewController.m
//  iOS-CtrlV
//
//  Created by 龚 杰 on 12-12-27.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "MyInformationViewController.h"
#import "FansAndAttentionViewController.h"
#import "MainTabBarController.h"
#import "SettingViewController.h"
#import "MyHttpProcessor.h"
#import "AsyncImageView.h"
#import "AlbumModel.h"
#import "UserInformationModel.h"
#import <QuartzCore/QuartzCore.h>

@interface MyInformationViewController ()

@end

@implementation MyInformationViewController

- (id)init
{
    if (self = [super init]) {
        // Custom initialization
        type = 1003;
        m_albumdata = [[NSMutableArray alloc] init];
    }
    return self;
}
- (id)initUesrID:(NSString*)_UesrID
{
    if (self = [super init]) {
        type = 1003;
        m_albumdata = [[NSMutableArray alloc] init];
        m_uesrID = _UesrID;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
	// Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithRed:193/255.f green:193/255.f blue:193/255.f alpha:1]];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
	// Do any additional setup after loading the view.
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:tAppDel.m_MidNavImg forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = NO;
        
        UIImageView* tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navshadow.png"]];
        tImgView.frame =CGRectMake(0, 44, 320, 5);
        [self.navigationController.navigationBar addSubview:tImgView];
        [tImgView release];
    }
    m_BackBttBarItem =[UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame =CGRectMake(0, 0, 44, 44);
    [m_BackBttBarItem setImage:[UIImage imageNamed:@"setting.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem addTarget:self action:@selector(ChangeSetting) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    
    [self CreateHeadView];
    
    m_postTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height-240) style:UITableViewStylePlain];
    m_postTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    m_postTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    m_postTableView.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
    [m_postTableView setDataSource:self];
    [m_postTableView setDelegate:self];
    
    
    if (type == 1003) {
        [m_postbutton setBackgroundColor:[UIColor whiteColor]];
        [m_albumbutton setBackgroundColor:[UIColor grayColor]];
        [m_goodbutton setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:m_postTableView];
    }else if(type == 1002){
        [m_postbutton setBackgroundColor:[UIColor grayColor]];
        [m_goodbutton setBackgroundColor:[UIColor whiteColor]];
        [m_albumbutton setBackgroundColor:[UIColor whiteColor]];
    }else if(type == 1004)
    {
        [m_postbutton setBackgroundColor:[UIColor whiteColor]];
        [m_goodbutton setBackgroundColor:[UIColor grayColor]];
        [m_albumbutton setBackgroundColor:[UIColor whiteColor]];
    }

    
    [[MyHttpProcessor sharedManager] RequestPersonalInformationGet:m_uesrID.integerValue ID:m_uesrID.integerValue];
//    [[MyHttpProcessor sharedManager] RequestAlbumListGet:userid ID:[NSNumber numberWithInt:userid].stringValue start:0 limit:1];
    [MyHttpProcessor sharedManager].delegate = self;
    [self addHudLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
    if ([m_BackBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    }
    MainTabBarController* tMainTabBar = (MainTabBarController*)self.navigationController.tabBarController;
    [tMainTabBar.m_TabbarCustom removeFromSuperview];
    [self.view addSubview:tMainTabBar.m_TabbarCustom];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [m_BackBttBarItem removeFromSuperview];
}
- (void)CreateHeadView
{
    m_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    m_head.backgroundColor = [UIColor whiteColor];
    
    AsyncImageView* haed = [[AsyncImageView alloc] initWithFrame:CGRectMake(10, 12, 80, 80)];
    //    haed.backgroundColor = [UIColor blackColor];
    haed.tag = 100;
    [m_head addSubview:haed];
    [haed release];
    
    UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, self.view.frame.size.width-130, 20)];
    name.tag = 101;
    name.backgroundColor = [UIColor clearColor];
    name.font = [UIFont boldSystemFontOfSize:18.0f];
    name.textColor = [UIColor blackColor];
    name.text = @"Arftgy";
    [m_head addSubview:name];
    [name release];
    
    
    
    UIButton* guan = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    guan.frame = CGRectMake(100, 52, 80, 40);
    guan.tag = 1000;
    [guan setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [guan setTitle:@"0 粉丝" forState:UIControlStateNormal];
    [guan addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [m_head addSubview:guan];
    
    UIButton* feng = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    feng.frame = CGRectMake(180,52,80,40);
    feng.tag = 1001;
    [feng setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [feng setTitle:@"0 关注" forState:UIControlStateNormal];
    [feng addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [m_head addSubview:feng];
    
    
    
    
    NSInteger w = self.view.frame.size.width/3;
    m_postbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    m_postbutton.layer.borderColor = [UIColor blackColor].CGColor;
    m_postbutton.layer.borderWidth = 1.0f;
    m_postbutton.frame = CGRectMake(0, 105,w , 45);
    m_postbutton.tag = 1002;
    [m_postbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [m_postbutton setTitle:@"0帖子" forState:UIControlStateNormal];
    [m_postbutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [m_head addSubview:m_postbutton];
    
    m_albumbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    m_albumbutton.layer.borderColor = [UIColor blackColor].CGColor;
    m_albumbutton.layer.borderWidth = 1.0f;
    m_albumbutton.frame = CGRectMake(w, 105,w , 45);
    m_albumbutton.tag = 1003;
    [m_albumbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [m_albumbutton setTitle:@"0专辑" forState:UIControlStateNormal];
    [m_albumbutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [m_head addSubview:m_albumbutton];
    
    m_goodbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    m_goodbutton.layer.borderColor = [UIColor blackColor].CGColor;
    m_goodbutton.layer.borderWidth = 1.0f;
    m_goodbutton.tag = 1004;
    m_goodbutton.frame = CGRectMake(w*2, 105, w+2, 45);
    m_goodbutton.titleLabel.textColor = [UIColor blackColor];
    [m_goodbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [m_goodbutton setTitle:@"0喜好" forState:UIControlStateNormal];
    [m_goodbutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [m_head addSubview:m_goodbutton];
    
    
    [self.view addSubview:m_head];
    
    
}
- (void)ChangeButton:(UIButton*)ibutton
{
    switch (ibutton.tag) {
        case 1000://粉丝
        {
            FansAndAttentionViewController* ifansandattent = [[FansAndAttentionViewController alloc] initType:FANSTYPE];
            ifansandattent.navigationItem.hidesBackButton =YES;
            ifansandattent.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:ifansandattent animated:YES];
            [ifansandattent release];
            break;
        }
        case 1001://关注
        {
            FansAndAttentionViewController* ifansandattent = [[FansAndAttentionViewController alloc] initType:ATTENTYP];
            ifansandattent.navigationItem.hidesBackButton =YES;
            ifansandattent.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:ifansandattent animated:YES];
            [ifansandattent release];
            break;
        }
        case 1002://帖子
        {
            [m_postbutton setBackgroundColor:[UIColor grayColor]];
            [m_goodbutton setBackgroundColor:[UIColor whiteColor]];
            [m_albumbutton setBackgroundColor:[UIColor whiteColor]];
            [m_postTableView removeFromSuperview];
            break;
        }
        case 1003://专辑
        {
            [m_postbutton setBackgroundColor:[UIColor whiteColor]];
            [m_goodbutton setBackgroundColor:[UIColor whiteColor]];
            [m_albumbutton setBackgroundColor:[UIColor grayColor]];
            if ([m_postTableView superview] == nil) {
                [self.view addSubview:m_postTableView];
            }
            break;
        }
            
        case 1004://喜好
        {
            [m_postbutton setBackgroundColor:[UIColor whiteColor]];
            [m_goodbutton setBackgroundColor:[UIColor grayColor]];
            [m_albumbutton setBackgroundColor:[UIColor whiteColor]];
            [m_postTableView removeFromSuperview];
            break;
        }
    }
}
- (void)ChangeSetting
{
    SettingViewController* setting = [[SettingViewController alloc] init];
    setting.navigationItem.hidesBackButton =YES;
    setting.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setting animated:YES];
    [setting release];
}

#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_albumdata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString* tSStrCell = @"FansAndAttentioncell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tSStrCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tSStrCell] autorelease];
        [tCell setBackgroundColor:[UIColor colorWithRed:193.0/255.0f green:193.0/255.0f blue:193.0/255.0f alpha:1.0f]];
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        for (int i = 0; i < 2; ++i) {
            if (i < [m_albumdata count]) {
                AlbumView* ialbmview = [[AlbumView alloc] initWithFrame:CGRectMake(20+20*i+130*i,10,130,160)];
                ialbmview.deleagate = self;
                ialbmview.tag = 100+i;
                [tCell.contentView addSubview:ialbmview];
            }
            
        }
        
    }
    for (int i = 0; i < 2; ++i) {
        if (i < [m_albumdata count]) {
            AlbumModel* amodel = [m_albumdata objectAtIndex:i+indexPath.row*2];
            AlbumView* iAlbumView = (AlbumView*)[tCell viewWithTag:100+i];
            iAlbumView.tag = i+indexPath.row*2;
            NSString* jie = [NSString stringWithFormat:@"%@ %d",amodel.albumtitle,amodel.count];
            NSLog(@"%@,%d",amodel.albumimgurl,amodel.albumimgurl.length);
            [iAlbumView setData:amodel.albumimgurl name:jie];
        }
        
    }
    
    
    return tCell;
}
#pragma mark AlbumViewDelegate
- (void)getAlbumViewTag:(AlbumView*)_albumView
{
    NSLog(@"tag%d",_albumView.tag);
}

#pragma mark MyHttpProcessorDelegate
- (void)OutPersonalInformationData:(UserInformationModel *)_data
{
    
    NSInteger userid = _data.user_id;
    UILabel* name = (UILabel*)[m_head viewWithTag:101];
    name.text = _data.uesr_name;
    AsyncImageView* asyimg = (AsyncImageView*)[m_head viewWithTag:100];
    asyimg._imageRequestURL = _data.user_headimg_url;
    asyimg.m_BoolSetImage = YES;
    [asyimg customLoadImageFromURL];
    UIButton* fans =(UIButton*)[m_head viewWithTag:1000];
    NSString* str = [NSString stringWithFormat:@"%@ 粉丝",_data.user_fans_count];
    [fans setTitle:str forState:UIControlStateNormal];
    
    
    UIButton* guan =(UIButton*)[m_head viewWithTag:1001];
    NSString* str1 = [NSString stringWithFormat:@"%@ 关注",_data.uesr_follow_count];
    [guan setTitle:str1 forState:UIControlStateNormal];
    
    NSString* post = [NSString stringWithFormat:@"%d帖子",_data.user_blog_count];
    [m_postbutton setTitle:post forState:UIControlStateNormal];
    
    NSString* album = [NSString stringWithFormat:@"%d专辑",_data.user_collect_count];
    [m_albumbutton setTitle:album forState:UIControlStateNormal];

    NSString* good = [NSString stringWithFormat:@"%d喜好",_data.user_like_count];
    [m_goodbutton setTitle:good forState:UIControlStateNormal];

}

- (void)OutPostListData:(NSMutableArray*)_data
{
    
}
- (void)OutAlbumListData:(NSMutableArray*)_data
{
    if ([_data count]>0) {
        [self hudWasHidden:nil];
        for (int i = 0; i < [_data count]; ++i) {
            [m_albumdata addObject:[_data objectAtIndex:i]];
        }
        [m_postTableView reloadData];
    }
}
- (void)OutGoodListData:(NSMutableArray*)_data
{
    
}
- (void)OutFansLisData:(NSMutableArray*)_data
{
    
}
- (void)OutAttentionListData:(NSMutableArray*)_data
{
    
}
- (void)OurErrer:(NSDictionary *)dic
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"errorMsg"] isCue:YES delayTime:3.0f];
    [self hudWasHidden:nil];
}
#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD.delegate = nil;
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.view];
		// Add HUD to screen
		[self.navigationController.view addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}
-(void)dealloc
{
    [m_head release];
    [m_postTableView release];
    m_postbutton = nil;
    m_goodbutton = nil;
    m_albumbutton = nil;
    m_BackBttBarItem = nil;
    [self hudWasHidden:nil];
    [super dealloc];
}


@end
